<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

use App\Article;

class ArticleController extends Controller
{
  //
  public function list(Request $request)
  {
    return view('admin.articleList')
            ->withArticles(Article::orderBy('id','desc')->paginate(20));
  }
  public function add(Request $request)
  {
    return view('admin.articleAdd')->withArticle('');
  }
  public function save(Request $request,$id=0)
  {
    if ( $id ) {
      $article = Article::find($id);
    } else {
      $article = new Article;
    }

    $article->title = $request->get('title');
    $article->author = $request->get('author');
    $article->enable = $request->get('enable');
    $article->type = $request->get('type');
    $article->content = $request->get('content');
    $article->describe = $request->get('describe');
    $article->created_at = $request->get('publish_time');
    $article->save();
    return response()->json(['status' => 0,'id' => $article->id]);
  }
  public function show(Request $request,$id)
  {
    $article = Article::find($id);
    return view('admin.articleAdd')
              ->withArticle($article);
  }
  public function update(Request $request,$id)
  {
    return $this->save($request,$id);
  }
  public function remove(Request $request,$id)
  {
    Article::find($id)->delete();
    return response()->json(['status' => 0]);
  }
}
